/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.impl;

import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.BusinessMessage;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionProtocol;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionProtocolElement;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionprotocolsPackage;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.Role;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.TimeExpresion;

import java.util.Collection;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;

import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;

import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.impl.EObjectImpl;

import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.InternalEList;


/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Interaction Protocol</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.impl.InteractionProtocolImpl#getName <em>Name</em>}</li>
 *   <li>{@link ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.impl.InteractionProtocolImpl#getStartEvent <em>Start Event</em>}</li>
 *   <li>{@link ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.impl.InteractionProtocolImpl#getPreCondition <em>Pre Condition</em>}</li>
 *   <li>{@link ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.impl.InteractionProtocolImpl#getPostCondition <em>Post Condition</em>}</li>
 *   <li>{@link ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.impl.InteractionProtocolImpl#getState <em>State</em>}</li>
 *   <li>{@link ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.impl.InteractionProtocolImpl#getIpElement <em>Ip Element</em>}</li>
 *   <li>{@link ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.impl.InteractionProtocolImpl#getTimeExpression <em>Time Expression</em>}</li>
 *   <li>{@link ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.impl.InteractionProtocolImpl#getRole <em>Role</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class InteractionProtocolImpl extends EObjectImpl implements InteractionProtocol {
	/**
	 * The default value of the '{@link #getName() <em>Name</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getName()
	 * @generated
	 * @ordered
	 */
	protected static final String NAME_EDEFAULT = null;

	/**
	 * The cached value of the '{@link #getName() <em>Name</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getName()
	 * @generated
	 * @ordered
	 */
	protected String name = NAME_EDEFAULT;

	/**
	 * The default value of the '{@link #getStartEvent() <em>Start Event</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getStartEvent()
	 * @generated
	 * @ordered
	 */
	protected static final String START_EVENT_EDEFAULT = null;

	/**
	 * The cached value of the '{@link #getStartEvent() <em>Start Event</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getStartEvent()
	 * @generated
	 * @ordered
	 */
	protected String startEvent = START_EVENT_EDEFAULT;

	/**
	 * The default value of the '{@link #getPreCondition() <em>Pre Condition</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getPreCondition()
	 * @generated
	 * @ordered
	 */
	protected static final String PRE_CONDITION_EDEFAULT = null;

	/**
	 * The cached value of the '{@link #getPreCondition() <em>Pre Condition</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getPreCondition()
	 * @generated
	 * @ordered
	 */
	protected String preCondition = PRE_CONDITION_EDEFAULT;

	/**
	 * The default value of the '{@link #getPostCondition() <em>Post Condition</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getPostCondition()
	 * @generated
	 * @ordered
	 */
	protected static final String POST_CONDITION_EDEFAULT = null;

	/**
	 * The cached value of the '{@link #getPostCondition() <em>Post Condition</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getPostCondition()
	 * @generated
	 * @ordered
	 */
	protected String postCondition = POST_CONDITION_EDEFAULT;

	/**
	 * The default value of the '{@link #getState() <em>State</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getState()
	 * @generated
	 * @ordered
	 */
	protected static final String STATE_EDEFAULT = null;

	/**
	 * The cached value of the '{@link #getState() <em>State</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getState()
	 * @generated
	 * @ordered
	 */
	protected String state = STATE_EDEFAULT;

	/**
	 * The cached value of the '{@link #getIpElement() <em>Ip Element</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getIpElement()
	 * @generated
	 * @ordered
	 */
	protected EList<InteractionProtocolElement> ipElement;

	/**
	 * The cached value of the '{@link #getTimeExpression() <em>Time Expression</em>}' containment reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getTimeExpression()
	 * @generated
	 * @ordered
	 */
	protected TimeExpresion timeExpression;

	/**
	 * The cached value of the '{@link #getRole() <em>Role</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getRole()
	 * @generated
	 * @ordered
	 */
	protected EList<Role> role;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected InteractionProtocolImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return InteractionprotocolsPackage.Literals.INTERACTION_PROTOCOL;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String getName() {
		return name;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setName(String newName) {
		String oldName = name;
		name = newName;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, InteractionprotocolsPackage.INTERACTION_PROTOCOL__NAME, oldName, name));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String getStartEvent() {
		return startEvent;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setStartEvent(String newStartEvent) {
		String oldStartEvent = startEvent;
		startEvent = newStartEvent;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, InteractionprotocolsPackage.INTERACTION_PROTOCOL__START_EVENT, oldStartEvent, startEvent));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String getPreCondition() {
		return preCondition;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setPreCondition(String newPreCondition) {
		String oldPreCondition = preCondition;
		preCondition = newPreCondition;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, InteractionprotocolsPackage.INTERACTION_PROTOCOL__PRE_CONDITION, oldPreCondition, preCondition));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String getPostCondition() {
		return postCondition;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setPostCondition(String newPostCondition) {
		String oldPostCondition = postCondition;
		postCondition = newPostCondition;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, InteractionprotocolsPackage.INTERACTION_PROTOCOL__POST_CONDITION, oldPostCondition, postCondition));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String getState() {
		return state;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setState(String newState) {
		String oldState = state;
		state = newState;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, InteractionprotocolsPackage.INTERACTION_PROTOCOL__STATE, oldState, state));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<InteractionProtocolElement> getIpElement() {
		if (ipElement == null) {
			ipElement = new EObjectContainmentEList<InteractionProtocolElement>(InteractionProtocolElement.class, this, InteractionprotocolsPackage.INTERACTION_PROTOCOL__IP_ELEMENT);
		}
		return ipElement;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public TimeExpresion getTimeExpression() {
		return timeExpression;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetTimeExpression(TimeExpresion newTimeExpression, NotificationChain msgs) {
		TimeExpresion oldTimeExpression = timeExpression;
		timeExpression = newTimeExpression;
		if (eNotificationRequired()) {
			ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, InteractionprotocolsPackage.INTERACTION_PROTOCOL__TIME_EXPRESSION, oldTimeExpression, newTimeExpression);
			if (msgs == null) msgs = notification; else msgs.add(notification);
		}
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setTimeExpression(TimeExpresion newTimeExpression) {
		if (newTimeExpression != timeExpression) {
			NotificationChain msgs = null;
			if (timeExpression != null)
				msgs = ((InternalEObject)timeExpression).eInverseRemove(this, EOPPOSITE_FEATURE_BASE - InteractionprotocolsPackage.INTERACTION_PROTOCOL__TIME_EXPRESSION, null, msgs);
			if (newTimeExpression != null)
				msgs = ((InternalEObject)newTimeExpression).eInverseAdd(this, EOPPOSITE_FEATURE_BASE - InteractionprotocolsPackage.INTERACTION_PROTOCOL__TIME_EXPRESSION, null, msgs);
			msgs = basicSetTimeExpression(newTimeExpression, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, InteractionprotocolsPackage.INTERACTION_PROTOCOL__TIME_EXPRESSION, newTimeExpression, newTimeExpression));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<Role> getRole() {
		if (role == null) {
			role = new EObjectContainmentEList<Role>(Role.class, this, InteractionprotocolsPackage.INTERACTION_PROTOCOL__ROLE);
		}
		return role;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__IP_ELEMENT:
				return ((InternalEList<?>)getIpElement()).basicRemove(otherEnd, msgs);
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__TIME_EXPRESSION:
				return basicSetTimeExpression(null, msgs);
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__ROLE:
				return ((InternalEList<?>)getRole()).basicRemove(otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__NAME:
				return getName();
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__START_EVENT:
				return getStartEvent();
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__PRE_CONDITION:
				return getPreCondition();
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__POST_CONDITION:
				return getPostCondition();
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__STATE:
				return getState();
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__IP_ELEMENT:
				return getIpElement();
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__TIME_EXPRESSION:
				return getTimeExpression();
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__ROLE:
				return getRole();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__NAME:
				setName((String)newValue);
				return;
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__START_EVENT:
				setStartEvent((String)newValue);
				return;
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__PRE_CONDITION:
				setPreCondition((String)newValue);
				return;
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__POST_CONDITION:
				setPostCondition((String)newValue);
				return;
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__STATE:
				setState((String)newValue);
				return;
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__IP_ELEMENT:
				getIpElement().clear();
				getIpElement().addAll((Collection<? extends InteractionProtocolElement>)newValue);
				return;
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__TIME_EXPRESSION:
				setTimeExpression((TimeExpresion)newValue);
				return;
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__ROLE:
				getRole().clear();
				getRole().addAll((Collection<? extends Role>)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__NAME:
				setName(NAME_EDEFAULT);
				return;
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__START_EVENT:
				setStartEvent(START_EVENT_EDEFAULT);
				return;
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__PRE_CONDITION:
				setPreCondition(PRE_CONDITION_EDEFAULT);
				return;
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__POST_CONDITION:
				setPostCondition(POST_CONDITION_EDEFAULT);
				return;
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__STATE:
				setState(STATE_EDEFAULT);
				return;
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__IP_ELEMENT:
				getIpElement().clear();
				return;
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__TIME_EXPRESSION:
				setTimeExpression((TimeExpresion)null);
				return;
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__ROLE:
				getRole().clear();
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__NAME:
				return NAME_EDEFAULT == null ? name != null : !NAME_EDEFAULT.equals(name);
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__START_EVENT:
				return START_EVENT_EDEFAULT == null ? startEvent != null : !START_EVENT_EDEFAULT.equals(startEvent);
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__PRE_CONDITION:
				return PRE_CONDITION_EDEFAULT == null ? preCondition != null : !PRE_CONDITION_EDEFAULT.equals(preCondition);
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__POST_CONDITION:
				return POST_CONDITION_EDEFAULT == null ? postCondition != null : !POST_CONDITION_EDEFAULT.equals(postCondition);
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__STATE:
				return STATE_EDEFAULT == null ? state != null : !STATE_EDEFAULT.equals(state);
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__IP_ELEMENT:
				return ipElement != null && !ipElement.isEmpty();
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__TIME_EXPRESSION:
				return timeExpression != null;
			case InteractionprotocolsPackage.INTERACTION_PROTOCOL__ROLE:
				return role != null && !role.isEmpty();
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (name: ");
		result.append(name);
		result.append(", startEvent: ");
		result.append(startEvent);
		result.append(", preCondition: ");
		result.append(preCondition);
		result.append(", postCondition: ");
		result.append(postCondition);
		result.append(", state: ");
		result.append(state);
		result.append(')');
		return result.toString();
	}
	
	@Override
	public EList<InteractionProtocolElement> getOrderedIpElement() {
		if (ipElement == null) {
			ipElement = new EObjectContainmentEList<InteractionProtocolElement>(InteractionProtocolElement.class, this, InteractionprotocolsPackage.INTERACTION_PROTOCOL__IP_ELEMENT);
			return ipElement;
		}
		
		EList<InteractionProtocolElement> ipElement = getRealIpElement();
		EList<InteractionProtocolElement> orderedIpElement =  new BasicEList<InteractionProtocolElement>();

		if (ipElement.size()==0)
			return orderedIpElement;

		InteractionProtocolElement middleElement = ipElement.get(0);
		if (middleElement==null)
			return orderedIpElement;
		
		InteractionProtocolElement actualElement = middleElement;
		orderedIpElement.add(actualElement);
		for (int i=1;i<ipElement.size();i++){
			actualElement = getPredecesor(actualElement, ipElement);
			
			if (actualElement==null)
				break;
			
			orderedIpElement.add(0, actualElement);

		}

		actualElement = middleElement;
		for (int i=1;i<ipElement.size();i++){
			actualElement = getSuccesor(actualElement, ipElement);
			
			if (actualElement==null)
				return orderedIpElement;
			
			orderedIpElement.add(actualElement);

		}

		return orderedIpElement;
		
	}
	
	private EList<InteractionProtocolElement> getRealIpElement() {
		if (ipElement == null) {
			ipElement = new EObjectContainmentEList<InteractionProtocolElement>(InteractionProtocolElement.class, this, InteractionprotocolsPackage.INTERACTION_PROTOCOL__IP_ELEMENT);
		}
		
		EList<InteractionProtocolElement> realIpElement = new BasicEList<InteractionProtocolElement>();
	
		for (InteractionProtocolElement element : ipElement){
			// Controla que ipElement no sea un business message que pertenece a un interaction path.-
			if (!(element instanceof BusinessMessage && ((BusinessMessage)element).getBmInteractionPath()!=null))
				realIpElement.add(element);
		}
		return realIpElement;
	}
	
	private InteractionProtocolElement  getPredecesor(InteractionProtocolElement actualElement, EList<InteractionProtocolElement> ipElement){
		
		for (int i=0;i<ipElement.size();i++){
			if (ipElement.get(i).getSuccessor()==actualElement)
				return ipElement.get(i);
		}
		return null;
	}

	private InteractionProtocolElement  getSuccesor(InteractionProtocolElement actualElement, EList<InteractionProtocolElement> ipElement){
		
		for (int i=0;i<ipElement.size();i++){
			if (ipElement.get(i).getPredecessor()==actualElement)
				return ipElement.get(i);
		}
		return null;
	}

} //InteractionProtocolImpl
